<?php
class ControllerPaymentKlarnaAccount extends AdminController {

	private $pclasses = array();

    public function index() {
		$this->document->setTitle($this->_['heading_title']);
        
		

		if ($this->request->isPost() && $this->validate()) {
			M('setting/setting');

			$status = false;			
			foreach ($this->request->post['klarna_account'] as $klarna_account) {
				if ($klarna_account['status']) {
					$status = true;					
					break;
				}
			}

			$data = array(
				'klarna_account_pclasses' => $this->pclasses,
				'klarna_account_status'   => $status
			);
			
			$this->model_setting_setting->editSetting('klarna_account', array_merge($this->request->post, $data));
            
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(U('extension/payment'));
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['text_home'],
            'href'      => UA('common/home'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['text_payment'],
            'href'      => UA('extension/payment'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['heading_title'],
            'href'      => UA('payment/klarna_account'),
            'separator' => ' :: '
        );
		
		$this->data['countries'] = array();
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_germany'],
			'code' => 'DEU'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_netherlands'],
			'code' => 'NLD'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_denmark'],
			'code' => 'DNK'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_sweden'],
			'code' => 'SWE'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_norway'],
			'code' => 'NOR'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_finland'],
			'code' => 'FIN'
		);
				
		if (isset($this->request->post['klarna_account'])) {
			$this->data['klarna_account'] = $this->request->post['klarna_account'];
		} else {
			$this->data['klarna_account'] = C('klarna_account');
		}
		
		M('localisation/geo_zone');			
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

		M('localisation/order_status');			
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
					        
		$file = DIR_LOGS . 'klarna_account.log';
		
        if (file_exists($file)) {
            $this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
        } else {
            $this->data['log'] = '';
        }
        
        $this->display('payment/klarna_account.tpl');
    }

    private function validate() {
        if (!$this->hasMP()) {
            $this->setMessage('errror_warning', $this->_['error_permission']);
            return false;
        }
		
        $log = new Log('klarna_account.log');
        
		$country = array(    
            'NOR' => array(
                'currency' => 1,
                'country'  => 164,
                'language' => 97,
            ),
            'SWE' => array(
                'currency' => 0,
                'country'  => 209,
                'language' => 138,
            ),
            'FIN' => array(
                'currency' => 2,
                'country'  => 73,
                'language' => 101,
            ),
            'DNK' => array(
                'currency' => 3,
                'country'  => 59,
                'language' => 27,
            ),
            'DEU' => array(
                'currency' => 2,
                'country'  => 81,
                'language' => 28,
            ),
            'NLD' => array(
                'currency' => 2,
                'country'  => 154,
                'language' => 101,
            ),
        );
        
        foreach ($this->request->post['klarna_account'] as $key => $klarna_account) {
            if ($klarna_account['status']) {
				$digest = base64_encode(pack("H*", hash('sha256', $klarna_account['merchant']  . ':' . $country[$key]['currency'] . ':' . $klarna_account['secret'])));
				
				$xml  = '<methodCall>';
				$xml .= '  <methodName>get_pclasses</methodName>';
				$xml .= '  <params>';
				$xml .= '    <param><value><string>4.1</string></value></param>';
				$xml .= '    <param><value><string>API:V5CART:' . VERSION . '</string></value></param>';
				$xml .= '    <param><value><int>' . (int)$klarna_account['merchant'] . '</int></value></param>';
				$xml .= '    <param><value><int>' . $country[$key]['currency'] . '</int></value></param>';
				$xml .= '    <param><value><string>' . $digest . '</string></value></param>';
				$xml .= '    <param><value><int>' . $country[$key]['country'] . '</int></value></param>';
				$xml .= '    <param><value><int>' . $country[$key]['language'] . '</int></value></param>';
				$xml .= '  </params>';
				$xml .= '</methodCall>';
				
				if ($klarna_account['server'] == 'live') {
					$url = 'https://payment.klarna.com';
				} else {
					$url = 'https://payment.testdrive.klarna.com';
				}
				
				$curl = curl_init();
	
				$header  = 'Content-Type: text/xml' . "\n";
				$header .= 'Content-Length: ' . strlen($xml) . "\n";
	
				curl_setopt($curl, CURLOPT_URL, $url);
				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($curl, CURLOPT_HEADER, $header);
				curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
	
				$response = curl_exec($curl);
				
				if ($response !== false) {
					$xml = new DOMDocument();
					$xml->loadXML($response);
	
					$xpath = new DOMXPath($xml);
	
					$nodes = $xpath->query('//methodResponse/params/param/value');
	
					if ($nodes->length == 0) {
						$this->error['warning'] = $this->language->get('error_log');
						
                    	$error_code = $xpath->query('//methodResponse/fault/value/struct/member/value/int')->item(0)->nodeValue;
                    	$error_message = $xpath->query('//methodResponse/fault/value/struct/member/value/string')->item(0)->nodeValue;
											
						$log->write(sprintf($this->language->get('error_pclass'), $key, $error_code, $error_message));
						
						continue;
					}
	
					$pclasses = $this->parseResponse($nodes->item(0)->firstChild, $xml);
	
					while ($pclasses) {
						$pclass = array_slice($pclasses, 0, 10);
						$pclasses = array_slice($pclasses, 10);
	
						$pclass[3] /= 100;
						$pclass[4] /= 100;
						$pclass[5] /= 100;
						$pclass[6] /= 100;
						$pclass[9] = ($pclass[9] != '-') ? strtotime($pclass[9]) : $pclass[9];
	
						array_unshift($pclass, $klarna_account['merchant']);
	
						$this->pclasses[$key][] = array(
							'eid'          => intval($pclass[0]),
							'id'           => intval($pclass[1]),
							'description'  => $pclass[2],
							'months'       => intval($pclass[3]),
							'startfee'     => floatval($pclass[4]),
							'invoicefee'   => floatval($pclass[5]),
							'interestrate' => floatval($pclass[6]),
							'minamount'    => floatval($pclass[7]),
							'country'      => intval($pclass[8]),
							'type'         => intval($pclass[9]),
						);
					}
				} else {
					$this->setMessage('error_warning', $this->_['error_log']);
					$log->write(sprintf($this->_['error_curl'], curl_errno($curl), curl_error($curl)));
					return false;
				}
				
				curl_close($curl);
			}
		}

        return true;
    }
	
    private function parseResponse($node, $document) {
        $child = $node;

        switch ($child->nodeName) {
            case 'string':
                $value = $child->nodeValue;
                break;
            case 'boolean':
                $value = (string)$child->nodeValue;

                if ($value == '0') {
                    $value = false;
                } elseif ($value == '1') {
                    $value = true;
                } else {
                    $value = null;
                }

                break;
            case 'integer':
            case 'int':
            case 'i4':
            case 'i8':
                $value = (int)$child->nodeValue;
                break;
            case 'array':
                $value = array();
                
                $xpath = new DOMXPath($document);
                $entries = $xpath->query('.//array/data/value', $child);
                
                for ($i = 0; $i < $entries->length; $i++) {
                    $value[] = $this->parseResponse($entries->item($i)->firstChild, $document);
                }

                break;
            default:
                $value = null;
        }
		
		return $value;
	}  
    
    public function clear() {
		$file = DIR_LOGS . 'klarna_account.log';		
		$handle = fopen($file, 'w+');				
		fclose($handle);
		
		$this->session->set_flashdata('success', $this->_['text_success']);
        $this->redirect(UA('payment/klarna_account'));
    }
}
?>
